import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/authentication.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:intl/intl.dart';
import 'PlaceController.dart';
import 'PlaceCustomStepper.dart';
import 'PlaceStepFour.dart';
import 'PlaceStepOne.dart';
import 'PlaceStepThreeSection.dart';
import 'PlaceStepTwo.dart';
import 'PlaceStepTwoEleven.dart';

class PlacePage extends GetView<PlaceController> {
  const PlacePage({this.isFromThirdParty, Key? key}) : super(key: key);
  static const String routeName = '/placement';
  final bool? isFromThirdParty;
  @override
  Widget build(BuildContext context) {
    MediaQuery.of(context).size.width;
    final PageStyles _pageStyles = PageStyles();
    final LoginController _loginController = Get.find();
    controller.username = _loginController.getUsername()!;
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(20),
        decoration: _pageStyles.pageBoxDecoration(),
        child: Column(
          children: [
            Row(children: [
              Column(children: [
                const SizedBox(height: 10),
                Image.asset('assets/images/icrLogo.png',
                    width: 200, height: 45),
                const SizedBox(height: 5),
              ]),
              Expanded(child: SizedBox()),
              FloatingActionButton(
                  onPressed: () {
                    if (controller.stepTop.value == 3) {
                      Future.delayed(Duration(milliseconds: 500), () {
                        if (controller.isStudent) {
                          Get.offAllNamed('/student/home');
                          Get.delete<PlaceController>();
                        } else {
                          Get.offAllNamed('/individual/home');
                          Get.delete<PlaceController>();
                        }
                      });
                    } else {
                      if (isFromThirdParty != null &&
                          isFromThirdParty == true) {
                        Future.delayed(Duration(milliseconds: 500), () {
                          Get.offAllNamed('/xlogin');
                          Get.delete<PlaceController>();
                        });
                      } else {
                        _loginController.logOut();
                        Future.delayed(Duration(milliseconds: 500), () {
                          Get.offAllNamed('/login');
                          Get.delete<PlaceController>();
                        });
                      }
                    }
                  },
                  backgroundColor: Theme.of(context).colorScheme.secondary,
                  child: const Icon(Icons.close_rounded),
                  tooltip: 'Close')
            ]),
            Obx(() {
              return StepsWidget(
                  currentIndex: controller.stepTop.value > 10
                      ? controller.stepTop.value - 10
                      : controller.stepTop.value);
            }),
            _buildPlaceMain(),
            Obx(() {
              return Visibility(
                  visible: controller.bottomBtnStatus > 0,
                  child: _buildButtonBottom());
            }),
            _buildPlaceBottom()
          ],
        ),
      ),
    );
  }

  Widget _buildPlaceMain() {
    Widget _getPlaceMain(int stepTop) {
      Widget placeMain = SizedBox();
      switch (stepTop) {
        case 0:
          placeMain = const PlaceStepOne();
          break;
        case 1:
          placeMain = const PlaceStepTwo();
          break;
        case 11:
          placeMain = const PlaceStepTwoEleven();
          break;
        case 2:
          placeMain = const PlaceStepThreeSection();
          break;
        case 3:
          placeMain = const PlaceStepFour();
          break;
      }
      return placeMain;
    }

    return Expanded(
        child: Container(
            margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.grey.withOpacity(0.2),
                width: 1,
              ),
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  blurRadius: 10,
                  spreadRadius: 0.1,
                  color: Colors.grey.withOpacity(0.2),
                ),
              ],
              borderRadius: BorderRadius.circular(10),
            ),
            width: double.infinity,
            child: Container(
                alignment: Alignment.topLeft,
                padding: const EdgeInsets.all(10),
                child: SingleChildScrollView(
                    padding: EdgeInsets.all(30),
                    child: Obx(() {
                      return AnimatedSwitcher(
                          transitionBuilder: (child, animation) {
                            var tween = Tween<Offset>(
                                begin: Offset(1, 0), end: Offset(0, 0));
                            return PlaceSlideTransition(
                                child: child,
                                position: tween.animate(animation));
                          },
                          duration: Duration(milliseconds: 300),
                          child: _getPlaceMain(controller.stepTop.value));
                    })))));
  }

  Widget _buildButtonBottom() {
    bool isEnable = controller.bottomBtnStatus.value == 2 ||
        controller.bottomBtnStatus.value == 4;
    return Container(
        alignment: Alignment.topCenter,
        height: 50,
        child: MaterialButton(
            minWidth: 130,
            height: 42,
            color: isEnable ? Color(0xffD2E5B5) : Color(0xffADCCC5),
            textColor: isEnable ? Color(0xff4FA397) : Color(0xff6A8A83),
            child: Text(controller.bottomBtnStatus > 2 ? 'CONTINUE' : 'NEXT',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.w900)),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6.0),
                side: BorderSide(
                  color: isEnable ? Color(0xffa4cc69) : Color(0xff8AAAA4),
                )),
            onPressed: () {
              if (isEnable) {
                if (controller.stepTop.value == 1) {
                  controller.stepTop.value = 11;
                  controller.bottomBtnStatus.value = 0;
                }
                if (controller.stepTop.value == 2) {
                  if (controller.bottomBtnStatus.value == 2)
                    controller.nextPage();
                  if (controller.bottomBtnStatus.value == 4)
                    controller.submitAction();
                }
              }
            }));
  }

  Widget _buildPlaceBottom() {
    return SizedBox(
        width: double.infinity,
        height: 20,
        child: Text(
            '© iChineseReader ${DateFormat('yyyy').format(DateTime.now()).toString()}',
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 16,
                color: Color(0xfff8f9fa),
                height: 1.5,
                fontWeight: FontWeight.w500)));
  }
}

class PlaceSlideTransition extends AnimatedWidget {
  PlaceSlideTransition({
    Key? key,
    required Animation<Offset> position,
    this.transformHitTests = true,
    required this.child,
  }) : super(key: key, listenable: position);

  Animation<Offset>? get position => listenable as Animation<Offset>;
  late final bool transformHitTests;
  late final Widget child;

  @override
  Widget build(BuildContext context) {
    Offset offset = position!.value;
    if (position!.status == AnimationStatus.reverse) {
      offset = Offset(-offset.dx, offset.dy);
    }
    return FractionalTranslation(
        translation: offset,
        transformHitTests: transformHitTests,
        child: child);
  }
}
